/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database.dao;

import database.common.GenericDao;
import database.entity.Categories;
import database.entity.Medicines;
import database.entity.Purchases;
import database.entity.StockDetails;
import java.math.BigDecimal;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transaction.ResinTransactionManagerLookup;

/**
 *
 * @author Administrator
 */
public class StockDetailsDao extends GenericDao<StockDetails, Integer> {

    public List<StockDetails> getAllByPurchase(Purchases aThis) {
        List<StockDetails> list;
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        Criteria c = session.createCriteria(StockDetails.class);
        c.add(Restrictions.eq("purchase", aThis));
        list = c.list();
        session.flush();
        session.getTransaction().commit();
        return list;
    }

    public List<StockDetails> getAllByMedicines(Medicines aThis) {
        List<StockDetails> list;
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        Criteria c = session.createCriteria(StockDetails.class);
        c.add(Restrictions.eq("medicine", aThis));
        list = c.list();
        session.flush();
        session.getTransaction().commit();
        return list;
    }

    public List<StockDetails> search(String name, Double price, Categories ct, String code) {
        List<StockDetails> list;
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        Criteria c = session.createCriteria(StockDetails.class);
        c.createAlias("medicine", "medicine_alias");
        if (!name.isEmpty()) {
            c.add(Restrictions.disjunction().add(Restrictions.like("medicine_alias.name", "%" + name + "%")));
        }
        if (!code.isEmpty()) {
            c.add(Restrictions.disjunction().add(Restrictions.like("medicine_alias.code", "%" + code + "%")));
        }
        if (ct != null && ct.getName() != "All Categories") {
            c.add(Restrictions.disjunction().add(Restrictions.eq("medicine_alias.category", ct)));
        }
        if (price > 0) {
            c.add(Restrictions.disjunction().add(Restrictions.le("medicine_alias.price", BigDecimal.valueOf(price))));
        }
        list = c.list();
        session.flush();
        session.getTransaction().commit();
        return list;
    }
}
